package com.bootdo.report.controller;

import com.bootdo.common.service.LogService;
import com.bootdo.common.utils.DateUtils;
import com.bootdo.common.utils.StringUtils;
import com.bootdo.company.service.CompanyService;
import com.bootdo.company.service.PostService;
import com.bootdo.front.vo.Result;
import com.bootdo.report.domain.VisitDo;
import com.bootdo.report.vo.MainReportVo;
import com.bootdo.report.vo.VisitVo;
import com.bootdo.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/report")
public class MainReportController {

    @Autowired
    private LogService logService;

    @Autowired
    private CompanyService companyService;

    @Autowired
    UserService userService;

    @Autowired
    PostService postService;
    @ResponseBody
    @GetMapping("/mainReport")
    public Result mainReport(){
        MainReportVo mainReport = new MainReportVo();

        //日访问量统计
        VisitVo visit = new VisitVo();
        Map<Integer,Integer> hourVisit = new HashMap<>();
        visit.setHourVisit(hourVisit);
        mainReport.setVisit(visit);

        List<VisitDo> visitInfo = logService.getCurrentDayVisitInfo();
        int total=0;
        if(visitInfo != null && !visitInfo.isEmpty()){
            for(VisitDo v : visitInfo){
                hourVisit.put(Integer.valueOf(v.getTime()),v.getCount());
                total += v.getCount();
            }
        }

        //没有的值全补0
        for(int i =0;i<24;i++){
            if(!hourVisit.containsKey(i)){
                hourVisit.put(i,0);
            }
        }

        visit.setTotal(total);
        visit.setDescribe(DateUtils.format(new Date(),"yyyy年MM月dd日")+"访问总量");

        //企业数量统计
       int companyCount = companyService.count(null);
        mainReport.setCompanyCount(companyCount);

        //已注册人数
        int userCount = userService.count(null);
        mainReport.setUserCount(userCount);
        int postCount=postService.count();
        mainReport.setPostCount(postCount);

        return Result.ok(mainReport);
    }
}
